<?
/**票付通对接艾斯森
*2015-05-11 16:10
*by pzp
*测试环境接口地址：http://116.255.207.71:8080/utws/services/FSalerService?wsdl
*账户：FXTEST
*密钥：8c59d1214a659a7a6c277d371f8173d6
*商品名称：重渡沟景区
商品编码：SP000004
商品价格：95
*/
class AiSiSen{
	private $lid;
	public function __construct($lid){
		if($lid == '8085'){
			$this->AGENT_ID = '18638488567';
			$this->AGENT_KEY = 'fc4911d8793b4175a246d200e7c792c2';
			$this->client = new SoapClient('http://fenxiao.chongdugou.net/utws/services/FSalerService?wsdl'); //重渡沟
		}elseif($lid == '8062'){
			$this->AGENT_ID = 'FXS031';
			$this->AGENT_KEY = 'a3035f808d04912b8c568dc2c895ed2c';
			$this->client = new SoapClient('http://fx.jiguandong.com/utws/services/FSalerService?wsdl'); //鸡冠洞
		}
	}
	
	//获取服务器时间
	public function getDateTime(){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData/>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->getPlatDateTime($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			$platTime = (string)$Check_IN->platTime;
			return $platTime;
		}else{
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.json_encode($Check_IN));
			return 101;
		}
	}
	
	//获取产品信息
	public function getAgencyProductInfo(){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData><providerNo></providerNo><productCode></productCode><pageSize>50</pageSize><pageIndex>1</pageIndex></tranData>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->getAgencyProductInfo($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			return $Check_IN;
		}else{
			$repCode = $Check_IN->repCode;
			$repMsg = $Check_IN->repMsg;
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$repCode.":".$repMsg);
			return 101;
		}
	
	}
	
	//订单新增
	public function agencyOrderAdd($request_array){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData><merOrderNo>'.$request_array['pftordernum'].'</merOrderNo><merOrderTime>'.date('Y-m-d H:i:s').'</merOrderTime><merOrderPayType>'.$request_array['ispay'].'</merOrderPayType><firstTourDate>'.$request_array['UUplaytime'].'</firstTourDate><ownerName>'.$request_array['pftordername'].'</ownerName><ownerIDCard>'.$request_array['IDcar'].'</ownerIDCard><ownerMobile>'.$request_array['UUordertel'].'</ownerMobile><takeTicketMode>1</takeTicketMode><ownerPwd></ownerPwd><ticketIDCards>'.$request_array['IDcar'].'</ticketIDCards><productCode>'.$request_array['uuid'].'</productCode><productPrice>'.$request_array['tprice'].'</productPrice><productNum>'.$request_array['UUtnum'].'</productNum></tranData>';

		$error_log = $this->logfile($tranData);
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->agencyOrderAdd($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			return $Check_IN;
		}else{
			$repCode = $Check_IN->repCode;
			$repMsg = $Check_IN->repMsg;
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$repCode.":".$repMsg);
			return 101;
		}
	}
	
	//撤改订单
	public function EditOrderNum($modify_array){
		$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.json_encode($modify_array));
		$tranData ='<?xml version="1.0" encoding="UTF-8" ?><tranData><platOrderNo>'.$modify_array['APIOrdern'].'</platOrderNo><editMode>'.$modify_array['editMode'].'</editMode><productCode>'.$modify_array['uuid'].'</productCode><productNum>'.$modify_array['productNum'].'</productNum><elecCode></elecCode></tranData>';		
		$request['in0']= $this->ass_post($tranData);

		$reR = $this->client->agencyOrderEditProductNum($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			return 100;
		}else{
			$repCode = $Check_IN->repCode;
			$repMsg = $Check_IN->repMsg;
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$modify_array['platOrderNo'].":".$repCode.":".$repMsg);
			return 101;
		}
	
	}
	
	
	//重发电子码
	public function agencyOrderRepeatECode(){
		$tranData ='<?xml version="1.0" encoding="UTF-8" ?><tranData><platOrderNo>2250150512568205</platOrderNo><ownerMobile>15959110796</ownerMobile><elecCode></elecCode></tranData>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->agencyOrderRepeatECode($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			return $Check_IN;
		}else{
			$repCode = $Check_IN->repCode;
			$repMsg = $Check_IN->repMsg;
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$repCode.":".$repMsg);
			return 101;
		}
	
	}
	
	
	/*共用方法*/
	//拼装数据
	public function ass_post($tranData){	
	
		$signed = md5($tranData.$this->AGENT_KEY);
		$tranData_base64 = base64_encode($tranData);
		
		$req_xml = '<?xml version="1.0" encoding="UTF-8" ?><req><interfaceVersion>1.0.0.0</interfaceVersion><merchantNo>' . $this->AGENT_ID . '</merchantNo><reqSerialNo>1</reqSerialNo><reqTime>' . date('Y-m-d H:i:s') . '</reqTime><merVar></merVar><tranData>' . $tranData_base64 . '</tranData><signData>' . $signed . '</signData></req>';
	    
		return $req_xml;
	}
	//校验数据
	public function Check_IN($reR){
		$reR_xml = simplexml_load_string($reR);
		$reR_tranData = base64_decode((string)$reR_xml->tranData);
		$reR_signData = (string)$reR_xml->signData;
		$reR_tranData_md5 = md5($reR_tranData.$this->AGENT_KEY);
	
		if($reR_tranData_md5 == $reR_signData){
			$reR_tranData_xml= simplexml_load_string($reR_tranData);
			return $reR_tranData_xml;
		}else{
			return $reR_xml;
		}
	
	}
	//日志	
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/AiSiSen/'.$path.'.txt';
		$word = date("Y-m-d H:i:s")."|".$word;
		if(file_exists($filepath)){
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
		}
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
// $getDateTime =  $ass->getDateTime(); print_r($getDateTime);
// $OrderAdd = $ass->agencyOrderAdd();print_r($OrderAdd ); //2250150512568205,9863150512623919
// $EditProductNum  = $ass->EditOrderNum(); print_r($EditProductNum);
// $RepeatECode = $ass->agencyOrderRepeatECode();print_r($RepeatECode);
// $getProduct = $ass->getAgencyProductInfo();print_r($getProduct);
?>